
<?php 
/* 
Template Name: processing-delete-product ajax
*/
?>

<?php 
if ( ! current_user_can( 'list_users' ) ) 
    {
      wp_die( __( 'bạn không có quyền truy cập trang này! ' ) );

            global $current_user;
            get_currentuserinfo();
    }//if ( ! current_user_can( 'list_users' ) ) 
    else { 
                      if (isset($_POST['id_xoa'])) 
                      {
                        $id_may_xoa = $_POST['id_xoa'];
                        $wpdb->delete( 'wp_may', array( 'id_may' => $id_may_xoa ), array( '%d' ) );

                        // set quyen cho admin duoc quan lí may nay 

                    global $current_user;
                    get_currentuserinfo();
                    $id_admin_now =  $current_user->ID;

                    //$sql = 'SELECT * FROM wp_may order by id_may desc';
                   // $id_may_array = $wpdb->get_row("SELECT * FROM wp_may order by id_may desc", ARRAY_N);

                    //var_dump($id_may_array[0]);


                    $wpdb->delete( 
                                          'wp_user_wp_may', 
                                          array( 
                                            'ID' => $id_admin_now, 
                                            'id_may' => $id_may_xoa,
                                          ), 
                                          array( 
                                            '%d', 
                                            '%d'
                                          ) 
                                          );


                        

                               $query_user = "SELECT * FROM wp_may" ;
                                                   
                                ?>            
                                                  <form >
                                                                  <table class="wp-list-table widefat fixed users" cellspacing="0">
                                                                    <thead>
                                                                      <tr>
                                                                        <th scope="col" id="cb" class="manage-column column-cb check-column" style=""> </th>
                                                                        <th scope="col" id="username"  style="text-align: left;">
                                                                            <span>Ảnh</span>
                                                                        </th>
                                                                        <th scope="col" id="name"  style="text-align: left;">
                                                                        <span>Tên máy</span>
                                                                        </th>
                                                                        <th scope="col" id="email" style=""></th>
                                                                      </tr>
                                                                    </thead>

                                                                    <tfoot>
                                                                    <tr>
                                                                      <th scope="col" style=""> </th>
                                                                      <th scope="col"  style="text-align: left;">
                                                                          <span>Ảnh</span>
                                                                      </th>
                                                                      <th scope="col" style="text-align: left;">
                                                                          <span >Tên máy</span>
                                                                      </th>
                                                                      <th scope="col" sstyle="text-align: left;position: relative;float: right;text-align: left;">
                                                                      </th>
                                                                    </tr>
                                                                    </tfoot>

                                                                    <tbody id="the-list" data-wp-lists="list:user">

                                                                  <?php 
                                                                     $rows = $wpdb->get_results($query_user);
                                                                    foreach($rows as $post)
                                                                      //var_dump($post);
                                                                    {

                                                                  ?>
                                                                    <tr id="product-<?php $post->id_may; ?>" class="alternate">

                                                                      <th scope="row" class="check-column"></th>

                                                                      <td >

                                                                    <?php if ($post->image_may_url =='')
                                                                     {
                                                                      ?>
                                                                       <img alt="<?php echo $post->name_may;?>" src="<?php echo get_bloginfo('template_directory')?>/images/no_image.gif" class="avatar avatar-32 photo" height="40" width="40" style="/* padding-left: 122px; */"> 
                                                                      <?php
                                                                    }else { ?>
                                                                      <img alt="<?php echo $post->name_may;?>" src="<?php echo get_bloginfo('template_directory').'/upload_may/images/'.$post->image_may_url;?>" class="avatar avatar-32 photo" height="40" width="40" style="/* padding-left: 122px; */"> 
                                                                      <?php } ?>
                                                                      </td>
                                                                    

                                                                           
                                                                       <td class="name column-name" style="text-align: left;font-weight: bold;"><?php echo $post->name_may;?></td>
                                                                        <td class="email column-email" style="display: inline-block;width: 297px;">
                                                                               <form><fieldset></fieldset></form>
                                                                       <div style="display: inline-block;width: 300px;padding-top: 8px;">
                                                                        <div style='float:left;'>
                                                                               <form action='<?php bloginfo('url');?>/edit-product' method='POST'>
                                                                                  <fieldset>
                                                                                    <input type="hidden" name="edit_prod" id="edit_prod" value='<?php echo $post->id_may; ?>'>
                                                                                    <input type="submit" name="edit_<?php echo $post->id_may;?>" id="edit_<?php echo $post->id_may;?>" value='Sửa'/>
                                                                                  </fieldset>
                                                                              </form>
                                                                        </div>
                                                                        <div style='float:left;'>
                                                                            <form id='xoa-form' method='POST'>
                                                                                <fieldset>
                                                                                  <nav role="tab">
                                                                                    <ul>
                                                                                      <li idxoa='<?php echo $post->id_may; ?>'>
                                                                               <input 
                                                                               type="button" name="xoa" id="xoa" value='Xóa' 
                                                                               idxoa='<?php echo $post->id_may;?>' 
                                                                               name='idxoa<?php echo $post->id_may;?>'
                                                                               />
                                                                                      </li>
                                                                                    </ul>
                                                                                  </nav>
                                                                                <!-- onclick="if (confirm('Bạn muốn xóa máy này ? chọn Click OK . Nếu bạn muốn xóa chọn HỦY')) return true;" -->
                                                                                </fieldset>
                                                                            </form>
                                                                        </div><!-- style='float:left;' -->
                                                                        <div style='float:left;'>
                                                                         <form action='<?php bloginfo('url');?>/manager-product-set' method='POST'>
                                                                          
                                                                      <input type='hidden' name='phanquyen' id='phanquyen' value='<?php echo $post->id_may;?>'/>
                                                                      <input  class="edit" type='submit' name='phanquyen_sumit' value='Phân quyền'/>
                                                                      
                                                                        </form>
                                                                      </div>

                                                                      </div>

                                                                        </td>


                                                                     
                                                                   </tr><!-- id="user-xyz" class="alternate" -->

                                                                  <?php } // end foreach($rows as $post) ?>


                                                                   </tbody>
                                                                  </table>
                              
                                         </form>
                               <script type="text/javascript">
                            
                                 jQuery(document).ready(function(){

                                      $("nav[role='tab'] ul li").click(function() {
                                      var id_xoa = $(this).attr("idxoa");
                                     // var UrL  = "<?php bloginfo('url');?>/quan-li-may";
                                     
                                     // alert(id_xoa);
                                     
                                     // return;

                                      $.ajax({
                                              url:"<?php bloginfo('url');?>/processing-delete-product",
                                                type:"POST",
                                                data: {id_xoa:id_xoa},
                                                success: function(data)
                                                {
                                                  
                                                $('#quanlimay').html(data);
                                                $('#quanlimay').show();
                                                }
                                          });
                                      });
                                        
                                    })
                                </script>
                    <?php 

                        echo ' <a href="/TTNaNoNangLuong/quan-li-may" style="color:red; magin-left:40px; font-size:15px;">
                               <br/> <h2>xóa máy thành công</h2></a>';
                        return;
                       }

 } //elseif ( ! current_user_can( 'list_users' ) )  ?>
    
